clear all
set more off 
version 12.0
set trace off
cap log close
pause off

* Set path
if "`c(username)'"=="YOUR USER NAME"{
	global home "YOUR PATH TO REPLICATION FOLDER"
}
	global data "$home/Data"
	
* NOTE: Data downloaded from WB Microdata Catalog https://microdata.worldbank.org/index.php/catalog/3774 should be 
* extracted to $home/Data/KEN_2020_COVIDRS_v07_M_STATA14

////////
// Load RRPS data
////////

* Household data

use "$data/KEN_2020_COVIDRS_v07_M_STATA14/hh.dta", clear

keep hhid-strata week_trend s2_q24_numchild s2_q15b_schoolchildren s2_q15c_youngchildren s2_q2_age s2_q3_gender ///
	s2_q9a_county s2_q15a_adults s2q_17_hhmove s2_q17_e head_gender head_age ///
	s2_q18_adults_gone s2_q19_floormat s2_q20_wallmat s2_q29_hhhmaritalstatus s2_nochildren s2_numhabrms ///
	s2_q21a_powergrid s2_q21b_poweruse s2_hasinternet s2_q23_anyphone s2_q14_hhhead_rel s2_q29_hhheduc ///
	ownasset_0-ownasset_7 ownasset_curr_0-ownasset_curr_7 s3_10_school_activity_yn-s3_q12b_wherenoschool__77 ///
	s3_q13_age s3_q16_schoolmeal s3_q17_schoolmeal_fr s4_q1_agactivity-s4_q7_plantamt s4_q1_agactivity-s4_q7_plantamt ///
	s4_q2_hours_allchild s4_q2b_fr_hrs s4_q3_outsideworker s4_q3b_outsidewrk_prev s4_q4_acres s4_q5_maincrop s4_q6_cropphase ///
	s4_q8_harvestamt s4_q9_livestockvalue s4_q10_agearnings s4_q10_agearningsfeb s4_q12_num_ent ///
	s4_q12_r1_oth s4_q12_num_r1_oth s4_q19_earnings_r1_* s4_q19_earnings_new_* s4_q20_salestrend_num_new_* s4_q20_profits_r1_* s4_q20_profits_new_* s4_q21a_profits_r1_* ///
	s4_q22_hours_allchild_r1 s4_q22_hours_check s4_36_self_hrsperweek_* s4_nohh_employment s4_chcarerhs_othact s4_chldcrhrs_children s4_chldcrhrs_outsidehh s4_q10a_agearningstrend ///
	s4_typempl s4_q36b_childcarerhs_self s4_q21a_profits_new_* s4_q22_hours_allchild_new ///
	s4_q19_earnings_r1_oth_* s4_q20_trend_num_r1_o_* s4_q20_profits_r1_oth_* s4_q22_hrs_allchild_r1_o s4_35_work_self s4_36_self_hrsperweek ///
	s4_q19_earnings_* s4_q20_profits_* s4_q21a_profits_* s4_q22_hours_allchild ///
	s5_q4_rent s5_q9c_whatchange_suff-s5_q41b_nofoodchild

tempfile rrps
save `rrps', replace

* Survey date from file received from WB survey team

use "$data/dates.dta", clear
merge 1:1 hhid wave using `rrps'
drop if _merge==1
drop _merge

tempfile rrps
save `rrps', replace

* Adult and child data

use "$data/KEN_2020_COVIDRS_v07_M_STATA14/hh_adult.dta", clear

ren adult_id individual

append using "$data/KEN_2020_COVIDRS_v07_M_STATA14/hh_child.dta"

replace individual=child_id+100 if missing(individual)
ren s4_q2_hours_allchild child_ag_hours
ren s4_chcarerhs_hhm s4_chldcrhrs_hhmem

keep hhid individual wave age-is_respondent marital relation childcare_hr s4_chcarerhs_othact ///
	mmbrs_lastyr__77 mmbrs_lastyr__98 whoemployed__77 whoemployed__98 education_level enrollment whenjoined ///
	tourism contract_type hours_march numworkers work_mode possible_remote wholaidoff laidoff_tourism laidoff_when laidoff_why backtowork ///
	whoemployed mmbrs_lastyr_ s4_chldcrhrs_hhmem childcare_hr_pre ///
	enter_hrs_old_resp_ hours_hh_lastyr__77 hours_hh_lastyr__98 hours_hh_lastyr_ education_feb ///
	workinghhmem_ workinghhmem__77 notworkinghhmem_ notworkinghhmem__77 enter_hrs_old workbeforemarch ///
	whoemployed_ agri_hrs enter_hrs sector hoursnow hoursthen earningnow earningthen est_sector  ///
	total_hours emp_wage emp_agri emp_enter employed unemployed nilf ///
	child_id-s3_chldcare_rel s3_attnpreschool s3b_childlabbfrlock-s3_hh_nonfarm_b ///
	child_ag_hours s4_chldcrhrs_outsidemmbrs_1-s4_chldcrhrs_outsidemmbrs__77 ///
	child_id schooltype returndate_school_ under_5__ btw_5_11__ above_12__ school_level_ school_level_now_ enroled_feb2020__ provided_chldcr_ ///
	s3_resumeschool_-s3_attndpast7d_ s3_q12b_oncereopen s3_q12b_whynoreatt s3_q11_atndbefmarch*
	
ren s3_q12b_oncereopen s2c_q4_oncereopen
forvalues i=1/7 {
	ren s3_q12b_wherenoschool_`i' s2c_q5_wherenoschool_`i'
}
ren s3_chldcare s3_chldcare_child

merge m:1 hhid wave using `rrps'
drop _merge
ren hhid id
sort id wave individual

////////
// Data Prep
////////

keep if strata==6 | strata==7 // drop refugee samples
drop if wave>4

// Initial cleaning

* Kids
replace s3_q10_atndbefmarch=1 if school_level_>0 & !missing(school_level_)
replace s3_q10_atndbefmarch=0 if school_level_==0
replace s3_q10_atndbefmarch=. if age>18
replace school_level_=. if wave==3
replace s3_q12_schoolgo=1 if s2c_q4_oncereopen==1
replace s3_q12_schoolgo=0 if s2c_q4_oncereopen==2 | s2c_q4_oncereopen==3
replace s3_q12b_oncereopen=1 if s2c_q4_oncereopen==1 | s2c_q4_oncereopen==2
replace s3_q12b_oncereopen=0 if s2c_q4_oncereopen==3
forvalues i=1/7 {
	replace s3_q12b_wherenoschool_`i' = s2c_q5_wherenoschool_`i' if missing(s3_q12b_wherenoschool_`i')
	drop s2c_q5_wherenoschool_`i'
}

* Respondent-only data in HH data file
gen resp=!missing(relation)
replace resp=1 if is_respondent==1
drop is_respondent
replace age=s2_q2_age if missing(age) & resp==1
replace gender=s2_q3_gender if missing(gender) & resp==1
drop s2_q2_age s2_q3_gender
replace ishead=1 if s2_q17_e==individual & missing(ishead)
replace ishead=0 if s2_q17_e!=individual & missing(ishead)
drop s2_q17_e
foreach var of varlist s4_q2b_fr_hrs s4_q36b_childcarerhs_self s4_35_work_self s4_36_self_hrsperweek {
	replace `var'=. if resp==0
}

* Work
replace whoemployed_=1 if !missing(hoursnow) & hoursnow!=0
replace hoursnow=0 if missing(hoursnow)
replace earningnow=0 if missing(earningnow)
replace hoursthen=0 if missing(hoursthen)
replace earningthen=0 if missing(earningthen)
ren enter_hrs enter_hrs_
replace enter_hrs_=enter_hrs_old if missing(enter_hrs_)
drop enter_hrs_old
ren agri_hrs agri_hrs_
replace s4_35_work_self=1 if s4_36_self_hrsperweek>0 & !missing(s4_36_self_hrsperweek)
replace s4_35_work_self=0 if s4_36_self_hrsperweek==0
replace s4_36_self_hrsperweek=0 if s4_35_work_self==0 
replace childcare_hr=s4_chldcrhrs_hhmem if missing(childcare_hr)
drop s4_chldcrhrs_hhmem
replace workinghhmem_=1 if workbeforemarch==1
la var workinghhmem_ "Working before March 2020"
la var workbeforemarch "Worked in same wage job before March 2020" 
ren s2_q29_hhheduc head_educ
ren s2_q29_hhhmaritalstatus head_marital
egen ent_earnings=rowtotal(s4_q19_earnings_r1_* s4_q19_earnings_new_* s4_q19_earnings_r1_oth_* s4_q19_earnings_*)
egen ent_profits=rowtotal(s4_q20_profits_r1_* s4_q20_profits_new_* s4_q20_profits_r1_oth_* s4_q20_profits_*)

* Apply variables across households
foreach var of varlist s2q_17_hhmove s2_q18_adults_gone s2_q19_floormat s2_q20_wallmat s2_q21a_powergrid ///
	ownasset_* s2_q23_anyphone s3_q10_atndbefmarch s4_q10_agearningsfeb ///
	s2_q9a_county head_gender head_age head_educ head_marital s3_q11_schooltype {
	egen `var'_max=max(`var'), by(id)
	replace `var'=`var'_max if missing(`var')
}
foreach var of varlist age gender ishead marital relation education_level s4_35_work_self ///
	hoursthen earningthen s4_36_self_hrsperweek workinghhmem_ workbeforemarch ///
	hours_hh_lastyr_ mmbrs_lastyr_ {
	egen `var'_max=max(`var'), by(id individual)
	replace `var'=`var'_max if missing(`var')
}
drop age_max gender_max
foreach var of varlist s3_chldcare_rel s3_q12b_whynoreatt under_5__ btw_5_11__ above_12__ ///
	school_level_ enroled_feb2020__ s3_q11_atndbefmarch_* age gender {
	egen `var'_max=max(`var'), by(id child_id)
	replace `var'=`var'_max if missing(`var')
}
replace enroled_feb2020__=1 if school_level_>0 & !missing(school_level_)
replace enroled_feb2020__=0 if school_level_==0
drop *_max 

// Variable prep
su education_level marital s2_q19_floormat s2_q20_wallmat s4_q6_cropphase s4_q12_num_ent ///
	whoemployed_ hoursnow agri_hrs_ if age>24 & age<46
bys wave: su agri_hrs
gen ed_prim= (education_level>=2)
gen ed_sec= (education_level>=4)
gen ed_postsec = education_level>4
replace ed_prim=. if missing(education_level)
replace ed_sec=. if missing(education_level)
replace ed_postsec=. if missing(education_level)
gen with_spouse=(marital==1 | marital==3 | marital==5)
replace with_spouse=. if missing(marital)
gen married=(marital<=4)
replace married=. if missing(marital)
gen floor_nice=(s2_q19_floormat>=5 | s2_q19_floormat==3)
gen wall_nice=(s2_q20_wallmat>=12 & s2_q20_wallmat<=17)
egen ent_lw_total=max(s4_q12_num_ent), by(id wave)
replace ent_lw_total=. if ent_lw_total>2
gen public_skl=(s3_q11_schooltype==1 | s3_q11_schooltype==3)
replace public_skl=. if s2_q15b_schoolchildren==0
gen private_skl=(s3_q11_schooltype==2 | s3_q11_schooltype==3)
replace private_skl=. if s2_q15b_schoolchildren==0

ren workinghhmem_ workinghhmem_feb
drop notworkinghhmem_
ren mmbrs_lastyr_ hhag_feb
ren s4_q2b_fr_hrs hhag_hours_feb
replace hhag_feb=1 if hhag_hours_feb>0 & !missing(hhag_hours_feb)
ren hours_hh_lastyr_ hhent_feb
ren enter_hrs_old_resp_ hhent_hours_feb
replace hhent_feb=1 if hhent_hours_feb>0 & !missing(hhent_hours_feb)
ren earningthen wage_earning_feb
ren hoursthen wage_hours_feb
replace wage_hours_feb=hours_march if !missing(hours_march)
drop hours_march
gen wage_feb=((wage_earning_feb>0 & !missing(wage_earning_feb)) | (wage_hours_feb>0 & !missing(wage_hours_feb)) | workbeforemarch==1)
replace wage_feb=. if missing(wage_earning_feb) & missing(wage_hours_feb)
drop workbeforemarch
replace workinghhmem_feb=1 if s4_35_work_self==1
foreach v of varlist hhag_feb hhent_feb wage_feb {
	replace workinghhmem_feb=1 if `v'==1
}
ren s4_35_work_self workself_feb
ren s4_36_self_hrsperweek workself_hours_feb
foreach var of varlist agri_hrs_ enter_hrs_ hoursnow earningnow wage_earning_feb wage_hours_feb wage_feb workinghhmem_feb {
	replace `var'=0 if missing(`var')
	replace `var'=. if age<18
}	
foreach var of varlist hhag_feb hhag_hours_feb hhent_feb hhent_hours_feb {
	replace `var'=0 if missing(`var') & wave==4 & resp==1
}
gen working_l7=(whoemployed_==1 | (hoursnow>0 & !missing(hoursnow)) | (agri_hrs_>0 & !missing(agri_hrs_)) | (enter_hrs_>0 & !missing(enter_hrs_)))  
egen workinghrs_l7=rowtotal(hoursnow agri_hrs_ enter_hrs_)  

gen kids=(s2_q15c_youngchildren>0 | s2_q15b_schoolchildren>0)
gen schoolkids=s2_q15b_schoolchildren>0 
gen youngkids=s2_q15c_youngchildren>0 
gen phone=s2_q23_anyphone>0
foreach v of varlist floor_nice wall_nice ownasset_1 ownasset_2 ownasset_4 phone {
	sum `v',d
	gen `v'_norm=(`v'-`r(mean)')/`r(sd)'
}
egen wealth_index=rowtotal(*_norm)
sum wealth_index,d
gen wealth_index_norm=(wealth_index-`r(mean)')/`r(sd)'

la var working_l7 "Engaged in any work in last 7 days"
la var whoemployed_ "Engaged in wage employment in last 7 days"
la var workinghrs_l7		"Total work hours, last 7 days"
la var hoursnow				"Wage hours, last 7 days"
la var agri_hrs_			"Ag hours, last 7 days"
la var enter_hrs_			"Enterprise hours, last 7 days"
la var childcare_hr			"Childcare hours, last 7 days"
la var gender				"Female"

gen adultgender=gender if age>17
egen adult_women=sum(adultgender), by(id wave)
gen adultmale=adultgender==0
egen adult_men=sum(adultmale), by(id wave)
gen current_num_adults=adult_women+adult_men
drop adultgender adultmale

gen adol_girl=age>9 & age<18 & gender==1
replace adol_girl=. if wave<3
gen adol_boy=age>9 & age<18 & gender==0
replace adol_boy=. if wave<3
egen adolescent_girls=sum(adol_girl), by(id wave)
egen adolescent_girls2=max(adolescent_girls), by(id)
egen adolescent_boys=sum(adol_boy), by(id wave)
egen adolescent_boys2=max(adolescent_boys), by(id)
egen adolescents = rowtotal(adolescent_boys adolescent_girls)
egen adolescents2 = rowtotal(adolescent_boys2 adolescent_girls2)
replace adolescents=. if wave<3
replace adolescents=adolescents2 if missing(adolescents)
la var adolescents "Count of adolescent (10-17) children"
gen child_age= age if !missing(child_id)
egen oldest_child_age=max(child_age), by(id wave)
egen oldest_child_age2=max(child_age), by(id)
replace oldest_child_age=oldest_child_age2 if missing(oldest_child_age)
replace oldest_child_age=round(oldest_child_age)
egen youngest_child_age=min(child_age), by(id wave)
egen youngest_child_age2=min(child_age), by(id)
replace youngest_child_age=youngest_child_age2 if missing(youngest_child_age)
replace youngest_child_age=round(youngest_child_age)
drop oldest_child_age2 youngest_child_age2

gen oldest_attending=school_level_>0 & school_level_<18
replace oldest_attending=. if round(age)!=oldest_child_age
ta age oldest_attending,mi
egen oldest_attend_skl=max(oldest_attending), by(id)
ta oldest_child_age oldest_attend_skl,mi
